package ModelLayer;

import java.util.ArrayList;

public class AddressBook
{   
    private static AddressBook instance;
    private ArrayList<Person>persons;

    /**
     * Constructor for objects of class AdressBook
     */
     private AddressBook()
    {
      persons=new ArrayList<Person>();
    }
    
    
    /**
     * Singleton.
     */
    
    public static AddressBook getInstance(){
        
        if(instance==null){
            instance=new AddressBook();
        }
        return instance;
    }
    
    
    /**
     * Add person.
     */
    
    public void addPerson(Person addPerson){
        
        persons.add(addPerson);
    }
    
    
    /**
     * Create new person.
     */
    
    public boolean createNewPerson(long id,String name,String address,String postalCode,String city,String phone){
       
        boolean found=false;
        boolean success=false;
       int index=0;
       while(index<persons.size()&& !found){
           Person a=persons.get(index);
           if(id==a.getId()){
              found=true;
            }
            else{
            index++;
          }
         }
         if(found==false){
       Person person=new Person(id,name,address,postalCode,city,phone);
       addPerson(person);
       System.out.println("Person was created");
       success=true;
     
         }
         else
         {
             System.out.println("There is already a person with that id");
            }
      return success;
     }
     
     
     /**
      * Read about a person.
      */
     
     public void readPerson(long id){
         
         boolean found=false;
         int index=0;
         int theIndex=0;
         while(index<persons.size()&&!found)
         {
             Person a =persons.get(index);
             if(a.getId()==id){
                 found=true;
                 persons.get(index).print();
                }
                else{
                    index++;
                }
            }
            if(found==false){
               System.out.println("There's no person with that ID");
             
            
         } 
        }
        
        /**
         * Find person
         */
        
        public Person findPerson(long id)
        {
            int index=0;
            boolean found=false;
            while(index<persons.size()&&!found)
            {
                Person p = persons.get(index);
                if(p.getId() == id)
                {
                    found=true;
                    return p;
                }
                else index++;
            }
            System.out.println("There is no person with that id");
            return null;
        }
            
        
        
            
            /**
             * Update person Name
             * 
             */
            
   public void updatePersonName(long id, String name)
   {
       int index=0;
       boolean found=false;
       while(index<persons.size()&&!found)
       {
           {Person p = persons.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setName(name);
                   System.out.println("Name Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            }
            
        }
        if (found==false)
        {
            System.out.println("There is no person with this ID");
        }
    }
    
     /**
             * Update person Adress
             * 
             */
    public void updatePersonAddress(long id, String address)
    {
       int index=0;
       boolean found=false;
       while(index<persons.size()&&!found)
       {
           {Person p = persons.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setAddress(address);
                   System.out.println("Address Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            }
            
        }
        if (found==false)
        {
            System.out.println("There is no person with this ID");
        }
    }
    
     /**
             * Update person postal code
             * 
             */
    public void updatePersonPostalCode(long id, String postalCode)
   {
       int index=0;
       boolean found=false;
       while(index<persons.size()&&!found)
       {
           {Person p = persons.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setPostalCode(postalCode);
                   System.out.println("Postal Code Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            }
            
        }
        if (found==false)
        {
            System.out.println("There is no person with this ID");
        }
    }
    
     /**
             * Update person city
             * 
             */
    public void updatePersonCity(long id, String city)
   {
       int index=0;
       boolean found=false;
       while(index<persons.size()&&!found)
       {
           {Person p = persons.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setCity(city);
                   System.out.println("City Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            }
            
        }
        if (found==false)
        {
            System.out.println("There is no person with this ID");
        }
    }
        
    
    
     /**
             * Update person Phone
             * 
             */
    public void updatePersonPhone(long id, String phone)
   {
       int index=0;
       boolean found=false;
       while(index<persons.size()&&!found)
       {
           Person p = persons.get(index);
           
               if(p.getId()==id)
               {
                   found=true;
                   p.setPhone(phone);
                   System.out.println("Phone Updated Succesfully");
                   break;
                }
                else
                {
                    index++;
                }
            
            
        }
        if (found==false)
        {
            System.out.println("There is no person with this ID");
        }
    }
    
     /**
             * Delete person
             * 
             */
            
    public void deletePerson(long id)
    {
        int index = 0;
        boolean found=false;
        while(index<persons.size()&&!found)
        {Person p = persons.get(index);
            if(p.getId()==id)
            {
                found=true;
                persons.remove(index);
                System.out.println("Person with id " + id + " was removed.");
                break;
            }
            else 
            {
                index++;
            }
        }
        if (found==false)
        {
            System.out.println("There was no person with this id");
        }
    }
    
    public ArrayList getPersons()
    {
        return persons;
    }
    
    public void listAllPersons()
    {
            
          System.out.println("ID   Name   Address  Postal Code     City    Phone Number");
          for(Person p : persons)
          {
              System.out.print(p.getId() + "   " + p.getName() + "   "  + p.getAddress() + "   " + p.getPostalCode() + "   ");
              System.out.print(p.getCity() + "    " + p.getPhone());
              System.out.print("\n");
            }
        }
            
}




        

  

